//
//  ViewController.m
//  20-HomeworkXML
//
//  Created by dllo on 16/8/10.
//  Copyright © 2016年 LST. All rights reserved.
//

#import "ViewController.h"
#import "GDataXMLNode.h"
#import "Model.h"
#import "XMLReader.h"
@interface ViewController ()
@property (nonatomic, strong)NSMutableArray *arr;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(50, 100, 100, 50);
    btn.backgroundColor = [UIColor yellowColor];
    [btn setTitle:@"点击有事" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.view addSubview:btn];
    [btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
 }

- (void)clickBtn:(UIButton *)sender {
    NSString *path = [[NSBundle mainBundle]pathForResource:@"Cartoon" ofType:@"xml"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSError *error = nil;
    GDataXMLDocument *document = [[GDataXMLDocument alloc] initWithData:data options:0 error:&error];
    GDataXMLElement *rootElement = [document rootElement];
    self.arr = [NSMutableArray array];
    for (GDataXMLElement *childElement in rootElement.children) {
        Model *model = [[Model alloc] init];
        for (GDataXMLElement *subElement in childElement.children) {
            if ([subElement.name isEqualToString:@"name"]) {
                model.name = subElement.stringValue;
            }
            [model setValue:subElement.stringValue forKey:subElement.name];
            [model setValue:subElement.stringValue forKeyPath:subElement.name];
        }
        NSLog(@"%@", childElement);
    }
    NSDictionary *xmlDic = [XMLReader dictionaryForXMLData:data error:&error];
    NSLog(@"%@", xmlDic);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
